<template>
  <div class="body">
    <div class="title">
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        style="width: 100%"
      >
        <el-form-item>
          <el-input
            v-model="formInline.title"
            placeholder="根据用户名搜索"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="formInline.title = ''">清除</el-button>

          <el-button type="primary" @click="onSearch">搜索</el-button>
        </el-form-item>

        <el-form-item style="float: right; margin-right: 0">
          <el-button
            type="success"
            icon="el-icon-edit"
            @click="addFn"
            >新增权限组</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="alert"><i class="el-icon-info"></i> 共{{ counts }}条记录</div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      highlight-current-row
      tooltip-effect="dark"
      style="width: 100%"
      :header-cell-style="{
        background: '#fafafa',
        color: '#909399',
        borderBottom: '2px #ccc solid',
      }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="60"> </el-table-column>
      <el-table-column prop="title" label="用户名" align="center">
      </el-table-column>
      <el-table-column sortable label="日期" >
        <template slot-scope="scope">{{ disponseTime(scope.row.create_date) }}</template>
      </el-table-column>
      <el-table-column prop="address" label="操作" width="150">
        <template slot-scope="{ row }">
          <el-button type="success" icon="el-icon-edit" circle @click="change(row.id)"></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="del(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      style="text-align: right; margin-top: 18px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pages.page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pages.pagesize"
      layout="  prev, pager, next,sizes, jumper"
      :total="counts"
    >
    </el-pagination>

    <PermissionsAdd ref='xiaomi'
    :text="title"
    :pageTitle="pageTitle"
    :ruleInline="ruleInline"
    @handleCloseModal="btnCancel"
    @newDataes="newDataes"
    :id="id"
    ></PermissionsAdd>
  </div>
</template>

<script>
import { list, remove, update, add, detail } from '@/api/base/permissions.js'
import PermissionsAdd from '../components/permissions-add.vue'

export default {
  components: { PermissionsAdd },
  data () {
    return {
      title: '编辑权限组',
      pageTitle: '新增权限组',
      ruleInline: { title: [{ required: true, message: '用户名不能为空' }] },
      tableData: [],
      pages: {
        page: 1,
        pagesize: 10
      },
      counts: 0,
      formInline: { title: '' },
      dialogFormVisible: false,
      form: {
        name: ''
      },
      rules: { name: [{ required: true, message: '用户名不能为空' }] },
      // 接收权限的数据
      // 先默认写一个死数据 让
      permData: [],
      // 权限中子树件的数据
      defaultProps: {
        label: 'title',
        children: 'childs'
      },
      handleNodeClick (data) {
        console.log(data)
      },
      id: null

    }
  },
  created () {
    this.list()
  },
  methods: {
    // 搜索框
    async onSearch () {
      this.pages = {
        page: 1,
        pagesize: 10,
        ...this.formInline
      }
      const { data } = await list({ ...this.pages })
      this.tableData = data.list
    },
    // 数据渲染列表
    async list () {
      const { data } = await list(this.pages)
      // console.log(data)
      this.counts = data.counts
      this.tableData = data.list
    },
    newDataes () {
      this.list()
    },
    disponseTime (str) {
      return str.split('.')[0].replace(/T/, ' ')
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.pages.pagesize = val
      this.list()
    },
    handleCurrentChange (val) {
      this.pages.page = val
      this.list()
    },
    // 删除功能
    async del (id) {
      try {
        await this.$confirm('您确定要删除吗?')
        await remove(id)
        this.list()
        this.$message.success('成功删除了用户')
      } catch (err) {
        console.log(err)
      }
    },
    // // 添加功能
    async addFn () {
      this.id = null
      this.$refs.xiaomi.dialogFormV()
    },
    // 修改功能
    async change (id) {
      this.id = id
      this.$refs.xiaomi.dialogFormV()
    },
    btnCancel () {
      this.$refs.xiaomi.dialogFormH()
    }

  }
}

</script>

<style scoped lang="less">
.body {
  box-sizing: border-box;
  padding: 20px;
  max-width: 100%;
  margin: 10px;
  background-color: #fff;
  color: #303133;
  .alert {
    height: 35px;
    width: 100%;
    padding-left: 16px;
    margin: 15px 0;
    border-radius: 3px;
    background-color: #f4f4f4;
    line-height: 35px;
    color: #909399;
    font-size: 13px;
  }
}
</style>
